package repository

import (
	"context"
	"errors"
	v1 "server_go/api/v1"
	"server_go/internal/model"

	"gorm.io/gorm"
)

type AdminRepository interface {
	GetAccount(ctx context.Context, account string) (*model.User, error)
}

func NewAdminRepository(repository *Repository) AdminRepository {
	return &adminRepository{
		Repository: repository,
	}
}

type adminRepository struct {
	*Repository
}

func (r *adminRepository) GetAccount(ctx context.Context, account string) (*model.User, error) {
	var admin model.User
	if err := r.DB(ctx).Where("account = ?", account).First(&admin).Error; err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return nil, v1.ErrNotFound
		}
		return nil, err
	}
	return &admin, nil
}
